<template>
<Upload ref="uploadButton" :action="picurl" :headers="headers" :default-file-list="fileList" :on-success="handleSuccess" :on-format-error="handleFormatError" :before-upload="handleBeforeUpload">
    <Button type="ghost" icon="ios-cloud-upload-outline">选择文件</Button>
</Upload>
</template>
<script>
export default {
    props: {
        fileList: {
            type: Array,
            default: []
        }
    },
    data () {
        return {
            headers: {
                Authorization: 'Bearer ' + (localStorage[this.$project.axios.Authorization] || '')
            },
            picurl: `${this.$project.axios.baseUrl}/api/user/v1/upload`
        }
    },
    methods: {
        
        /************************************************************
         * 
         * 上传文件成功时
         * 
         ************************************************************/
        handleSuccess (response, file, fileList) {
            this.$emit('uploadData', response.data)
        },

        /************************************************************
         * 
         * 上传文件失败时
         * 
         ************************************************************/
        handleFormatError (file, fileList) {
            this.$Message.error({
                content: '上传文件失败',
                duration: 2
            });
        },

        /************************************************************
         * 
         * 上传文件前
         * 
         ************************************************************/
        handleBeforeUpload (data) {
            this.$refs.uploadButton.clearFiles()
        }
    },
    created(){
    }
}
</script>